<?php

class User_ViewController extends Zend_Controller_Action{
	private $userId;
	
	public function preDispatch(){
		if(Zend_Auth::getInstance()->getIdentity()){
			$this->userId = Zend_Auth::getInstance()->getIdentity()->id;
		}
	}
	
	public function indexAction(){
		
		$form = new Zend_Form(array(
			'action'	=>	'/user/view',
			'method'	=>	'post'
		));
		
		
		
		$request = $this->getRequest();
		if($request->isPost()){
			if($form->isValid($request->getPost())){
				$data = $request->getPost();
				if(array_key_exists('setAddress',$data)){
					$this->view->message = Doctrine_Model_UserAddresses::setAddress($this->userId, $data);
				}
				if(array_key_exists('setContact',$data)){
					$this->view->message = Doctrine_Model_UserContacts::setContact($this->userId, $data);
				}
				if(array_key_exists('setPayment',$data)){
					$this->view->message = Doctrine_Model_UserPayments::setPayment($this->userId, $data);	
				}
			}
		}
		
		$userAddressData = Doctrine_Model_UserAddresses::getAddress($this->userId);
		$userAddressForm = new Custom_Form_Address();
		$userAddressForm->populate($userAddressData);
		$form->addSubForm($userAddressForm, 'Address');
		
		$userContactData = Doctrine_Model_UserContacts::getContact($this->userId);
		$userContactForm = new Custom_Form_Contact();
		$userContactForm->populate($userContactData);
		$form->addSubForm($userContactForm, 'Contact');
		
		$userPaymentData = Doctrine_Model_UserPayments::getPayment($this->userId);
		$userPaymentForm = new Custom_Form_Payment();
		$userPaymentForm->populate($userPaymentData);
		$form->addSubForm($userPaymentForm, 'Payment');
		$this->view->form = $form;
	}
}